package com.hb.guli.service.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.utils.StringUtils;
import com.aliyuncs.vod.model.v20170321.DeleteStreamRequest;
import com.aliyuncs.vod.model.v20170321.DeleteStreamResponse;
import com.google.common.collect.Lists;
import com.hb.guli.common.base.result.ResultCodeEnum;
import com.hb.guli.service.base.exception.GuliException;
import com.hb.guli.service.vod.service.VideoService;
import com.hb.guli.service.vod.util.AliyunVodSDKUtils;
import com.hb.guli.service.vod.util.VodProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VodProperties vodProperties;

    @Override
    public String upload(InputStream inputStream, String originalFilename) {
        final String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
        UploadStreamRequest request = new UploadStreamRequest(
                vodProperties.getKeyid(), vodProperties.getKeysecret(),
                title, originalFilename, inputStream);

        /* 模板组ID(可选) */
        request.setTemplateGroupId(vodProperties.getTemplateGroupId());
        /* 工作流ID(可选) */
        //request.setWorkflowId("d4430d07361f0*be1339577859b0177b");
        /* 存储区域(可选) */
        //request.setStorageLocation("in-201703232118266-5sejdln9o.oss-cn-shanghai.aliyuncs.com");

        /* 设置应用ID*/
        //request.setAppId("app-1000000");
        /* 点播服务接入点 */
        //request.setApiRegionId("cn-shanghai");
        /* ECS部署区域*/
        // request.setEcsRegionId("cn-shanghai");
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        final String videoId = response.getVideoId();
        if (StringUtils.isEmpty(videoId)) {
            log.error("aliyun 上传失败：" + response.getCode() + "-" + response.getMessage());
            throw new GuliException(ResultCodeEnum.VIDEO_UPLOAD_ALIYUN_ERROR);
        }
        return videoId;
    }

    @Override
    public void removeVideo(String videoId) throws ClientException {
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(vodProperties.getKeyid(), vodProperties.getKeysecret());

        DeleteStreamRequest request = new DeleteStreamRequest();
        request.setVideoId(videoId);
        client.getAcsResponse(request);
    }

    @Override
    public void removeVideoByIdList(List<String> videoByIdList) throws ClientException {
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(vodProperties.getKeyid(), vodProperties.getKeysecret());

        DeleteStreamRequest request = new DeleteStreamRequest();
        final int size = videoByIdList.size();
        if(size <= 20){
            final String join = StringUtils.join(",", videoByIdList);
            request.setVideoId(join);
        }else{
            final List<List<String>> partition = Lists.partition(videoByIdList, 19);
            partition.forEach(part -> {
                final String join = String.join(",", part);
                request.setVideoId(join);
            });
        }
        client.getAcsResponse(request);
    }


    /**
     * 删除媒体流函数
     *
     * @param client 发送请求客户端
     * @return DeleteMezzaninesResponse 删除媒体流响应数据
     * @throws Exception
     */
    public static DeleteStreamResponse deleteStream(DefaultAcsClient client) throws Exception {
        DeleteStreamRequest request = new DeleteStreamRequest();
        request.setVideoId("VideoId");
        request.setJobIds("JobId1,JobId2");
        return client.getAcsResponse(request);
    }

}
